package com.example.tianxiaoyu.zyfypt__128txy.model;




import com.example.tianxiaoyu.zyfypt__128txy.bean.TwareBean;
import com.example.tianxiaoyu.zyfypt__128txy.listener.TwareListener;
import com.example.tianxiaoyu.zyfypt__128txy.iface.Twareiface;
import com.example.tianxiaoyu.zyfypt__128txy.service.TwareService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dell on 2017/4/19.
 */
//model,文章模块实现Articleiface
public class TwareModel implements Twareiface {

    private Retrofit  retrofit;//声明
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造方法
public TwareModel()
{
    //实例化Retrofit
retrofit=new Retrofit.Builder()
        .baseUrl(BASEURL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
}
//获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final TwareListener twareListener) {
        TwareService twareService
                =retrofit.create(TwareService.class);
        Call<List<TwareBean>> call
                =twareService.getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<TwareBean>>() {
            @Override
            public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                    twareListener.onResponse(response.body());
                else
                    twareListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                twareListener.onFail(t.toString());
            }
        });


    }
}
